// pages/discovery/discovery.js


import { getStar } from '../../utils/api'
// const myopenid = wx.getStorageSync('openid')

Page({

    /**
     * 页面的初始数据
     */
    data: {

        isLoading:false,
        conlist:[],
        zan:[],
        user_nickName:"",
        

    },

    //局部刷新
    bindDownLoad:function(){
        setTimeout(()=>{
            this.setData({
                isLoading:false
            })
            this.disStar()
            this.getUploaded() // 下拉刷新触发的事件  再次获取投稿列表
            },2000)
    },

 

    //下拉刷新
    onRefresh:function(){
        //导航条加载动画
        wx.showNavigationBarLoading()
        //loading 提示框
        wx.showLoading({
          title: 'Loading...',
        })
        console.log("下拉刷新啦");

        setTimeout(function () {
          wx.hideLoading();
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();

        }, 1000)

      },

  //获取点赞信息
  disStar:function(){

    getStar().then(res=>{
      this.setData({
        zan:res.data
      })
    })
  },

  //获取投稿列表
  getUploaded(){
    let openid = wx.getStorageSync('openid') 
    var that=this
     wx.request({
       url: 'http://localhost:3000/getUploaded',
       method:'GET',
       data:{
           openid:openid},
       success(res){
        if(res.data.code ==200){
            that.isGood(that.data.zan,res)  //区分本用户点赞和未点赞
            console.log(res);
            that.setData({
              conlist:res.data.draftList,
            })
            
            var temp;
            var i=0
            for (let i = 0; i < that.data.conlist.length / 2; i++) {
            temp = that.data.conlist[i];
            that.data.conlist[i] = that.data.conlist[that.data.conlist.length-1-i];
            that.data.conlist[that.data.conlist.length-1-i] = temp;
            res.data.draftList.map((item)=>{
              if(item.pote){
                item.pote = item.pote.split('/')
              }
            })
            that.setData({
                conlist:that.data.conlist
            })
            
}
            var i=0;
            var unionids=[]
            for(i=0;i<that.data.conlist.length;i++){
                unionids.push(that.data.conlist[i].union_id)
            }
           wx.setStorageSync('union_id', unionids)
          }else{
              
            console.log(err);
          }
       }
     })
  },

//跳转到编辑页面
editPage(e){
    let index=e.currentTarget.dataset.index
    wx.setStorageSync('index', index)
    wx.navigateTo({
      url: '../edit/edit',
    })
},
    //列表诗词详情转跳
    detailpote(e){
      let name = e.currentTarget.dataset.name
      let author = e.currentTarget.dataset.author
      console.log(name);
      wx.navigateTo({
        url: `../details/details?name=${JSON.stringify(name)}&author=${author}`

      })
    },
//   删除发表过的评论
deleteUploaded(e){
    let index=e.currentTarget.dataset.index;
    const union_id= this.data.conlist[index].union_id;
    // console.log(union_id)
    var that=this
    wx.request({
      url: 'http://localhost:3000/deleteUploaded',
      method:'POST',
      data:{
          union_id:union_id
        },
      success(res) {
        if(res.data.code ==200){
            console.log('删除成功')
            that.getUploaded()
        }else{
        }
      }    
    })

},
  


      //判断当前用户是否已经点赞
      isGood:function(zan,res){
        if(zan && res.draftList){
          for(var i =0;i<zan.length;i++){
            for(var key in res.draftList){
              if(zan[i].draft_id ==res.draftList[key].id){
        
                res.draftList[key].like_nums ++
                if( zan[i].openid == myopenid){

                  res.draftList[key].is_good = 1
                }

              }

            }
          }

        }
        
      },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.disStar()
        this.getUploaded()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
      this.disStar()
      this.getUploaded()

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.disStar()
      this.getUploaded()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})